<template>
	<!-- 添加弹框 -->
	<el-dialog  title="" width="50%"  :append-to-body="true" visible :show-close="false"  class="wrap" @close="cancel">
		<i class="el-icon-close close" @click="cancel" style="color: white;"></i>
	    <div class="bt">校区选择</div>
		<div class="form">
			<div class="tj2" v-for = 'i in dataList' @click="login(i.xxid)">{{i.xxmc}}校区</div>
			<!--<div class="tj1" v-for = 'i in dataList' @click="login(i.xxid)"></div>-->
		</div>
	</el-dialog>
</template>
<script type="text/javascript">
	import qs from 'qs'
	import md5 from 'js-md5';
	export default {
		props: ['params'],
		data() {
			return {
				comboxlist: [],
				loginBol: false,
				loading: false,
                error: '',
                loginTilte: '登录提示',
                loginHint: '请重新登录',
                loginContent: '',
				dialogTableVisible: true,
				dataList: [],
			}
		},
		created() {
			if(this.params){
				this.dh = this.params.dh
			    this.mm = this.params.mm
			    localStorage.setItem('logyhxx',JSON.stringify({dh:this.dh,mm:this.mm}))
			}
				this.getcomboxlist()
		},
		methods: {
			getcomboxlist() {
				var logyhxx = JSON.parse(localStorage.logyhxx)
	            this.searchLoading = true;
                var params2 = qs.stringify({
                      dh: logyhxx.dh, 
                      mm:md5(logyhxx.mm),
                      lx: '1',
                 })
                 this.axios.post(this.$store.state.api + 'ls/getxx',params2)
                 .then(res => {
//               	console.log(res,'sssssssssssss')
                 	if(res.data.code == 1) {
		                  let data = res.data.data;
		                  let list = data.list;
		                  this.dataList = list;
                  if(this.dataList.length==1){
                  	   this.login(this.dataList[0].xxid)
                  }
                  this.searchLoading = false;
                  }else{
                  	this.loading = false;
					this.loginContent = res.data.msg
					this.$emit('close', { type: 'dldgxq', cancel: true })
					this.messageAlert();
                  }
                 }).catch(res => {
                  this.searchLoading = false;
				 })
			},
			async login(xxid) {
				   var logyhxx = JSON.parse(localStorage.logyhxx)
					var params = qs.stringify({
						username: logyhxx.dh,
						password: md5(logyhxx.mm),
						xxid:xxid,
					})
					this.loading = true;
					this.error = '';
					this.axios.post(this.$store.state.api + 'log_in', params)
						.then(res => {
							var code = res.data.code;
							if(code == 1) {
								this.$store.state.isLogin = 100
									this.loginBol = true;
									// 写登录信息
									localStorage.setItem('logininfo',JSON.stringify(res.data.data.list[0]))
									var data1 = JSON.parse(localStorage.logininfo)
									this.messageAlert();
//									location.reload();
//									if(this.dataList.length==1){
//										this.messageAlert();
//									}else{
//										this.messageAlert();
//										location.reload();
//									}
						              //页面刷新代码
							} else {
								this.loading = false;
								this.loginContent = res.data.msg
								this.messageAlert();
							}
						}).catch(res => {
							this.loading = false;
							this.loginContent = '登录出错'
							this.messageAlert();
						})
			},
			messageAlert() {
				
				if(this.loginBol) {
					this.$router.push({ path: '/home' })
				} else {
					this.$alert(this.loginContent, this.loginTilte, {
						confirmButtonText: '确定',
						callback: action => {
							if(this.loginBol) {
								this.$router.push({ path: '/home'})
							} else {
								this.$message({type: 'danger',message: this.loginHint});
							}
						}
					});
				}
			},
			cancel() {
				this.$emit('close', { type: 'dldgxq', cancel: true })
			},
			componentsShow (type,data,list) { //显示组件
					for ( let i in this.portComponents ) {
							if (type == this.portComponents[i].type) {
									this.portComponents[i].toggle = true;
									this.portComponents[i].params = data;
									if (this.portComponents[i].data.type == 'single') {
											this.portComponents[i].data.index = data.row.id
									}else if(this.portComponents[i].data.type == 'multiple'){
											this.portComponents[i].data.index = this.multipleDelNum;
									}else{
											return;
									}
									return;
							}
					}
			},
			componentsHide (setting) { //关闭组件
					for ( let i in this.portComponents ) {
							if (setting.type == this.portComponents[i].type) {
									this.portComponents[i].toggle = false;
									if (!setting.cancel) {
									}
									return;
							}
					}
			}
		}
	}
</script>
<style lang="less" scoped >
	.wrap {
		.row{
			height:50px;
			line-height: 50px;
		}
		.bt{
			position: absolute;
			width: 100%;
			height: 50px;
			margin: 0 auto;
			top: 0;
			left: 0;
			// margin-top: -70px;
			background: #50BFFF;
			// margin-left: -20px;
			// margin-bottom: 20px;
			color:white;
			text-align:left;
			line-height: 50px;
			font-size: 17px;
			padding-left: 15px;
		}
		.close{
				z-index: 10;
				position: absolute;
		}
		.input-block {
			margin-bottom: 20px;
			text-align: center;
		}
		.input {
			width:280px;
			display: inline-block;
			margin-left: 10px;
		}
		.input1 {
			width: 210px;
			display: inline-block;
			margin-left: 10px;
		}
		.input-block span {
			display: inline-block;
			width: 70px;
			text-align: right;
		}
		.submit {
			// width: 120px;
			// display: block;
			margin-left:20%;
			margin-top: 20px;
		}
		.wpfl {
			background: #fff;
			position: absolute;
			top: 5px;
			left: 5px;
		}
		.wpfl1 {
			width: 130%;
			background: #fff;
			position: absolute;
			top: 5px;
			left: -66px;
		}
		.form {
			/*width: 63%;*/
			text-align:center ;
			/*border-right: 1px solid #e8e8e8;*/
			/*text-align: left;*/
			margin-top: 20px;
			.select-block {
				width: 80%;
				display: block;
			}
			.select-block1 {
				width: 68%;
				// display: block;
			}
		}
		.tj{
			width: 200px;
			height: 100px;
			/*position: absolute;
			top: 35%;
			right: 5%;*/
		}
		.tj2{
			width: 200px;
			height: 80px;
			margin: 0 auto;
			margin-top: 10px;
			text-align: center;
			background: #409EFF;
			color: white;
			font-size: 20px;
			line-height: 80px;
			cursor: pointer;
			/*position: absolute;*/
			/*top: 35%;*/
			/*right: 5%;*/
		}
	}
</style>
